package com.suray.rgv60;

import com.alibaba.fastjson.JSON;
import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.path.enums.NodeType;
import com.suray.basic.wcs.utils.Coord;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.rgv.IRgvManager;
import com.suray.rgv.RgvInterface;
import com.suray.rgv.instance.RgvInstance;
import com.suray.rgv60.generate.GeneralMapManage;
import com.suray.rgv60.generate.MapMsgManageHandle;
import com.suray.rgv60.generate.Rgv60MapStruct;
import com.suray.type.RgvMapTagDirection;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 两栖车构造器
 * 生成两栖车地图
 */
public class RgvCreater implements IRgvManager {

    protected MapMsgManageHandle mapMsgManageHandle;

    public RgvCreater() {
        mapMsgManageHandle = new MapMsgManageHandle();
    }

    @Override
    public RgvInterface newRgv(int rgvNo, String serialNum, String ip, Integer port) {
        return new Rgv60Impl(rgvNo, serialNum, ip);
    }

    @Override
    public void generalMap(String paperFile,List<Node> mapNodes, RgvMapTagDirection direction) throws IOException {
        Map<Integer, List<Rgv60MapStruct.Scope>> advancedAreaListMap = mapMsgManageHandle.getAdvancedAreaList(paperFile);
        mapMsgManageHandle.generalMapMsg(paperFile,mapNodes, direction,advancedAreaListMap);
    }

    @Override
    public void loadRgvSmap(Integer layer,String paperFile,InputStream inputStream, List<Node> mapNodes,
                            RgvMapTagDirection direction) throws IOException {
        Rgv60MapStruct rgv60MapStruct = GeneralMapManage.generateRgv60MapStruct(inputStream);
        String area = paperFile + File.separator + "wcs_area" + File.separator + "area_" + layer + ".area";
        WcsFuncUtil.writeFile(area,JSON.toJSONString(rgv60MapStruct.getAdvancedAreaList()));
        Map<Integer, Node> tagNodeMap = new HashMap<>();
        Map<Coord, Node> coordNodeMap = new HashMap<>();
        mapNodes.forEach(node -> {
            tagNodeMap.put(node.getGroundCode(), node);
            coordNodeMap.put(node, node);
        });

        Map<Integer, Rgv60MapStruct.TagPosListDTO> tagPosMap = new HashMap<>();

        rgv60MapStruct.getTagPosList().forEach(tagPosListDTO -> {
            tagPosMap.put(tagPosListDTO.getTagValue(), tagPosListDTO);
        });
        rgv60MapStruct.getTagPosList().forEach(tagPosListDTO -> {
            Node node = tagNodeMap.get(tagPosListDTO.getTagValue());
            //提升机节点地码唯一处理
            if(node.getNodeType() == NodeType.L){
                mapNodes.forEach(nodeLifter -> {
                    if(nodeLifter.getNodeType() == NodeType.L){
                        mapMsgManageHandle.analyzeUp(nodeLifter, tagPosListDTO, tagPosMap, coordNodeMap);
                        mapMsgManageHandle.analyzeLeft(nodeLifter, tagPosListDTO, tagPosMap, coordNodeMap);
                        nodeLifter.setSeerMapX((int) (tagPosListDTO.getX() * 1000));
                        nodeLifter.setSeerMapY((int) (tagPosListDTO.getY() * 1000));
                    }
                });
            }else {
                mapMsgManageHandle.analyzeUp(node, tagPosListDTO, tagPosMap, coordNodeMap);
                mapMsgManageHandle.analyzeLeft(node, tagPosListDTO, tagPosMap, coordNodeMap);
                node.setSeerMapX((int) (tagPosListDTO.getX() * 1000));
                node.setSeerMapY((int) (tagPosListDTO.getY() * 1000));
            }
        });
        generalMap(paperFile,mapNodes, direction);
    }

    @Override
    public void loadMapLayer(String paperFile) {
        List<File> mapFiles = GeneralMapManage.getMapFiles(paperFile);
        for(File file : mapFiles){
            if(file.getName().contains(".smap")){
                String mapName = file.getName().replace(".smap", "");
                Integer mapLayer = Integer.valueOf(mapName.split("_")[1]);
                Map<Integer, String> mapMsg = RgvInstance.rgvMapManage.getMap();
                if(mapMsg.get(mapLayer) == null){
                    RgvInstance.rgvMapManage.addMap(mapLayer,mapName);
                }
            }
        }
    }
}
